<template>
  <div class="common-page">
    <SearchForm
      :search-list="searchList"
      :search-params="searchParams"
      @search="onSearch"
      @reset="onReset"
    />
    <el-form>
      <el-form-item>
        <div style="text-align: right">
          <el-button type="primary" @click="onAdd">新建项目</el-button>
        </div>
      </el-form-item>
    </el-form>
    <div class="table-box">
      <el-table
        v-loading="loading"
        height="100%"
        :data="tableData"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.8)"
        @selection-change="onSelect"
      >
        <el-table-column v-for="item in columns" :key="item.prop" :prop="item.prop" :label="item.label" align="center" />
        <el-table-column label="项目状态">
          <template slot-scope="scope">
            <StatusTag :status="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="100px">
          <template slot-scope="scope">
            <el-button type="primary" @click="onUpload(scope.row)">资料上传</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-box">
      <el-pagination
        background
        :current-page="pageNo"
        :page-sizes="pageSizes"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalSize"
        @size-change="changePageSize"
        @current-change="changePageNo"
      />
    </div>
    <DataUpload ref="DataUploadRef" />
  </div>
</template>

<script>
import crud from '@/mixins/crud'
import { http } from '@/api'
import { ProjectStatus, getName, ProjectType } from '@/dict'
import DataUpload from '@/views/meeting/apply/data-upload'
export default {
  name: 'Index',
  components: {
    DataUpload
  },
  mixins: [crud],
  data() {
    return {
      request: http.enterprise_project,
      searchList: [
        {
          key: 'projectType',
          name: 'select',
          label: '项目类型',
          options: ProjectType
        },
        {
          key: 'capital',
          name: 'input',
          label: '投资方'
        },
        {
          key: 'timeValue',
          name: 'datetime',
          valueFormat: 'yyyy-MM-dd',
          dateType: 'daterange',
          label: '时间'
        },

        {
          key: 'projectName',
          name: 'input',
          label: '项目名称'
        },
        {
          key: 'pocket',
          name: 'input',
          label: '资金'
        }
      ],
      searchParams: {
        projectType: '',
        capital: '',
        projectName: '',
        timeValue: [],
        pocket: ''
      },
      columns: [
        { prop: 'projectName', label: '项目名称' },
        { prop: 'projectTypeName', label: '项目类型' },
        { prop: 'investorName', label: '项目内容' },
        { prop: 'pocket', label: '资金（万）' },
        { prop: 'capital', label: '投资方' },
        { prop: 'createTime', label: '上报时间' }
      ],
      tableData: [

      ]
    }
  },
  methods: {
    formatTableData(data) {
      data.forEach(el => {
        el.projectTypeName = getName(el.projectType, ProjectType)
        el.statusName = getName(el.status, ProjectStatus)
      })
    },
    getQueryParams() {
      const params = {
        pageNo: this.pageNo,
        pageSize: this.pageSize
      }

      if (!this.searchParams) return params
      for (const searchParamsKey in this.searchParams) {
        if (searchParamsKey === 'timeValue') {
          if (!this.searchParams[searchParamsKey] || !this.searchParams[searchParamsKey].length) {
            this.searchParams[searchParamsKey] = ['', '']
          }
          params['startTime'] = this.searchParams.timeValue[0] || undefined
          params['endTime'] = this.searchParams.timeValue[1] || undefined
        } else {
          params[searchParamsKey] = this.searchParams[searchParamsKey] === '' ? null : this.searchParams[searchParamsKey]
        }
      }
      return params
    },
      onAdd() {
          this.$router.push({
              path: '/project/add'
          })
      },
    onRead(item) {
      this.$router.push({
        path: '/project/detail',
        query: {
          id: item.id
        }
      })
    },
    onUpload(item) {
      this.$refs.DataUploadRef.open(item)
    }
  }
}
</script>

<style scoped lang="scss">
</style>
